package com.rhok.wthru_client;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.rhok.utils.LocationUtils;

public class MyMapActivity extends MapActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_map);

		MapView mv = (MapView) (findViewById(R.id.mapview));

		final MapController mapController = mv.getController();
		mapController.setZoom(17);
		mv.setBuiltInZoomControls(true);

		// Set to current location
		Location myLocation = GlobalConfiguration.getInstance(
				getApplicationContext()).getLastKnownLocation();
		if (myLocation != null) {

			Log.d("Maps", "Center map using location: " + myLocation.toString());

			GeoPoint gp = LocationUtils.locationToPoint(myLocation);

			// Center map to my location
			mapController.setCenter(gp);
			mapController.animateTo(gp);

			// --------------

			List<Overlay> mapOverlays = mv.getOverlays();
			Drawable drawable = this.getResources().getDrawable(
					R.drawable.map_pin);
			HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
					drawable, this);
			OverlayItem overlayitem = new OverlayItem(gp, "Me", "This is me!");
			itemizedoverlay.addOverlay(overlayitem);

			mapOverlays.add(itemizedoverlay);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_my_map, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
		private Context mContext;

		public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
			super(boundCenterBottom(defaultMarker));
			mContext = context;
		}

		public void addOverlay(OverlayItem overlay) {
			mOverlays.add(overlay);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mOverlays.get(i);
		}

		@Override
		public int size() {
			return mOverlays.size();
		}

		@Override
		protected boolean onTap(int index) {
			OverlayItem item = mOverlays.get(index);
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
			return true;
		}
	}

}
